package com.promote.algorithm;

/**
 * @ClassName MinSubArrayLen
 * Description 长度最小的子数组 --力扣209
 * @Author LiZiHao
 * Date 2021/11/11 14:59
 * @Version 1.0
 **/
public class MinSubArrayLen {

    public int minSubArrayLen(int s, int[] nums) {
        int n = nums.length;
        if (n==0) {
            return 0;
        }
        int ans = Integer.MAX_VALUE;
        int start = 0;
        int end = 0;
        int sum = 0;
        while (end < n) {
            sum += nums[end];
            while (sum >= s) {
                ans = Math.min(ans,end-start+1);
                sum -= nums[start];
                start++;
            }
            end++;
         }
        return ans == Integer.MAX_VALUE ? 0 : ans;
    }

}
